package com.swift.authority.service.menu.impl;


import com.swift.authority.common.constant.CommonConstant;
import com.swift.authority.core.menu.dto.MenuDto;
import com.swift.authority.core.menu.dto.SubMenuDto;
import com.swift.authority.core.menu.response.MenuRes;
import com.swift.authority.core.menu.response.SubMenu;
import com.swift.authority.mapper.menu.MenuMapper;
import com.swift.authority.service.menu.MenuService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author Administrator
 * @description :
 * @date 2022/06/18 16:27
 * @since open jdk 1.8
 */
@Service
public class MenuServiceImpl implements MenuService {
    private Logger logger = LoggerFactory.getLogger(MenuServiceImpl.class);

    @Resource
    private MenuMapper menuMapper;

    @Override
    public Map<String, List<MenuRes>> queryMenuInfo() {
        logger.info("获取菜单信息");
        return getMenuMap(menuMapper.queryMainMenuInfo(), menuMapper.querySubMenuInfo());
    }

    private static Map<String, List<MenuRes>> getMenuMap(List<MenuDto> menuDtoList, List<SubMenuDto> subMenuDtoList) {
        Map<String, List<MenuRes>> menuMap = new HashMap<>();
        List<MenuDto> menuList = new ArrayList<>();
        for (MenuDto mainMenu : menuDtoList) {
            List<SubMenuDto> subMenuDtos = new ArrayList<>();
            for (SubMenuDto subMenu : subMenuDtoList) {
                if (mainMenu.getMid() == subMenu.getParentId()) {
                    subMenuDtos.add(subMenu);
                    mainMenu.setSubMenuList(subMenuDtos);
                }
            }
            menuList.add(mainMenu);
        }
        //排序
        menuList.sort(Comparator.comparing(MenuDto::getMid).thenComparing(MenuDto::getMid));

        List<MenuRes> menuResList = new ArrayList<>();


        for (MenuDto menuDto : menuList) {
            MenuRes menuRes = new MenuRes();
            menuRes.setMid(menuDto.getMid());
            menuRes.setMenuName(menuDto.getMenuName());
            menuRes.setMenuUrl(menuDto.getMenuUrl());
            menuRes.setMenuDesc(menuDto.getMenuDesc());
            List<SubMenu> subMenus = new ArrayList<>();
            List<SubMenuDto> subMenuList = menuDto.getSubMenuList();
            if(subMenuList !=null ){
                for (SubMenuDto subMenuDto : subMenuList) {
                    SubMenu subMenu = new SubMenu();
                    subMenu.setSid(subMenuDto.getSid());
                    subMenu.setMenuName(subMenuDto.getMenuName());
                    subMenu.setParentId(subMenuDto.getParentId());
                    subMenu.setMenuUrl(subMenuDto.getMenuUrl());
                    subMenu.setMenuDesc(subMenuDto.getMenuDesc());
                    subMenus.add(subMenu);
                }
                menuRes.setSubMenuList(subMenus);
                menuResList.add(menuRes);
            }else{
                menuRes.setSubMenuList(null);
                menuResList.add(menuRes);
            }
        }

        menuMap.put(CommonConstant.MENUS_KEY, menuResList);
        return menuMap;
    }
}
